package com.quanyan.ic.task;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.ic.constants.JobConstant;
import com.quanyan.ic.task.job.FailedMsgCheckJob;
import com.quanyan.ic.task.job.SendICMessageJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by zhuo.shi on 2017/6/2.
 */
@Component
public class JobRunnerDispatcher implements JobRunner {

    private static Logger logger = LoggerFactory.getLogger(JobRunnerDispatcher.class);

    @Autowired
    private SendICMessageJob sendICMessageJob;
    @Autowired
    private FailedMsgCheckJob failedMsgCheckJob;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        String taskId = jobContext.getJob().getTaskId();
        logger.debug("定时器收到任务：{}", jobContext);

        if (taskId.startsWith(JobConstant.SEND_IC_MSG_TASK_ID)) {
            sendICMessageJob.run(jobContext);
        } else if (taskId.equals(JobConstant.FAILED_MSG_CHECK_TASK_ID)) {
            failedMsgCheckJob.run(jobContext);
        }
        return new Result(Action.EXECUTE_SUCCESS);
    }
}
